#include "DbType.h"
#include "utils/log.h"

DbType DbType::other = DbType(1ULL << 0, make_string_ptr("other"));
DbType DbType::jtds = DbType(1ULL << 1, make_string_ptr("jtds"));
DbType DbType::hsql = DbType(1ULL << 2, make_string_ptr("hsql"));
DbType DbType::db2 = DbType(1ULL << 3, make_string_ptr("db2"));
DbType DbType::postgresql = DbType(1ULL << 4, make_string_ptr("postgresql"));

DbType DbType::sqlserver = DbType(1ULL << 5, make_string_ptr("sqlserver"));
DbType DbType::oracle = DbType(1ULL << 6, make_string_ptr("oracle"));
DbType DbType::mysql = DbType(1ULL << 7, make_string_ptr("mysql"));
DbType DbType::mariadb = DbType(1ULL << 8, make_string_ptr("mariadb"));
DbType DbType::derby = DbType(1ULL << 9, make_string_ptr("derby"));

DbType DbType::hive = DbType(1ULL << 10, make_string_ptr("hive"));
DbType DbType::h2 = DbType(1ULL << 11, make_string_ptr("h2"));
DbType DbType::dm = DbType(1ULL << 12, make_string_ptr("dm")); // dm.jdbc.driver.DmDriver
DbType DbType::kingbase = DbType(1ULL << 13, make_string_ptr("kingbase"));
DbType DbType::gbase = DbType(1ULL << 14, make_string_ptr("gbase"));

DbType DbType::oceanbase = DbType(1ULL << 15, make_string_ptr("oceanbase"));
DbType DbType::informix = DbType(1ULL << 16, make_string_ptr("informix"));
DbType DbType::odps = DbType(1ULL << 17, make_string_ptr("odps"));
DbType DbType::teradata = DbType(1ULL << 18, make_string_ptr("teradata"));
DbType DbType::phoenix = DbType(1ULL << 19, make_string_ptr("phoenix"));

DbType DbType::edb = DbType(1ULL << 20, make_string_ptr("edb"));
DbType DbType::kylin = DbType(1ULL << 21, make_string_ptr("kylin")); // org.apache.kylin.jdbc.Driver
DbType DbType::sqlite = DbType(1ULL << 22, make_string_ptr("sqlite"));
DbType DbType::ads = DbType(1ULL << 23, make_string_ptr("ads"));
DbType DbType::presto = DbType(1ULL << 24, make_string_ptr("presto"));

DbType DbType::elastic_search = DbType(1ULL << 25, make_string_ptr("elastic_search")); // com.alibaba.xdriver.elastic.jdbc.ElasticDriver
DbType DbType::hbase = DbType(1ULL << 26, make_string_ptr("hbase"));
DbType DbType::drds = DbType(1ULL << 27, make_string_ptr("drds"));

DbType DbType::clickhouse = DbType(1ULL << 28, make_string_ptr("clickhouse"));
DbType DbType::blink = DbType(1ULL << 29, make_string_ptr("blink"));
DbType DbType::antspark = DbType(1ULL << 30, make_string_ptr("antspark"));
DbType DbType::oceanbase_oracle = DbType(1ULL << 31, make_string_ptr("oceanbase_oracle"));
DbType DbType::polardb = DbType(1ULL << 32, make_string_ptr("polardb"));

DbType DbType::ali_oracle = DbType(1ULL << 33, make_string_ptr("ali_oracle"));
DbType DbType::mock = DbType(1ULL << 34, make_string_ptr("mock"));
DbType DbType::sybase = DbType(1ULL << 35, make_string_ptr("sybase"));
DbType DbType::highgo = DbType(1ULL << 36, make_string_ptr("highgo"));
/**
 * 非常成熟的开源mpp数据库
 */
DbType DbType::greenplum = DbType(1ULL << 37, make_string_ptr("greenplum"));
/**
 * 华为的mpp数据库
 */
DbType DbType::gaussdb = DbType(1ULL << 38, make_string_ptr("gaussdb"));
DbType DbType::trino = DbType(1ULL << 39, make_string_ptr("trino"));
DbType DbType::oscar = DbType(1ULL << 40, make_string_ptr("oscar"));
DbType DbType::tidb = DbType(1ULL << 41, make_string_ptr("tidb"));
DbType DbType::tydb = DbType(1ULL << 42, make_string_ptr("tydb"));
DbType DbType::starrocks = DbType(1ULL << 43, make_string_ptr("starrocks"));
DbType DbType::ingres = DbType(1ULL << 44, make_string_ptr("ingres"));
DbType DbType::cloudscape = DbType(1ULL << 45, make_string_ptr("cloudscape"));
DbType DbType::timesten = DbType(1ULL << 46, make_string_ptr("timesten"));
DbType DbType::as400 = DbType(1ULL << 47, make_string_ptr("as400"));
DbType DbType::sapdb = DbType(1ULL << 48, make_string_ptr("sapdb"));
DbType DbType::kdb = DbType(0, make_string_ptr("kdb"));
DbType DbType::log4jdbc = DbType(1ULL << 49, make_string_ptr("log4jdbc"));
DbType DbType::xugu = DbType(1ULL << 50, make_string_ptr("xugu"));
DbType DbType::firebirdsql = DbType(1ULL << 51, make_string_ptr("firebirdsql"));
DbType DbType::JSQLConnect = DbType(1ULL << 52, make_string_ptr("JSQLConnect"));
DbType DbType::JTurbo = DbType(1ULL << 53, make_string_ptr("JTurbo"));
DbType DbType::interbase = DbType(1ULL << 54, make_string_ptr("interbase"));
DbType DbType::pointbase = DbType(1ULL << 55, make_string_ptr("pointbase"));
DbType DbType::edbc = DbType(1ULL << 56, make_string_ptr("edbc"));
DbType DbType::mimer = DbType(1ULL << 57, make_string_ptr("mimer"));

DbType::DbType(DbType *dbType)
{
  this->mask = dbType->mask;
  this->name = dbType->name;
}

DbType::DbType(uint64_t mask, string_ptr name)
{
  this->mask = mask;
  this->name = name;
}
// DbType& DbType::operator=(DbType& val) {
// 		_enum = val;
// 		currstr = str.find((int)val)->second;
// 		return _enum;
// }
// DbType& DbType::operator=(DbType& val){
// 		_enum=val._enum;
// 		currstr=str.find((int)val._enum)->second;
// 		return _enum;
// }
bool DbType::operator==(DbType &rgt)
{
  return name == rgt.name && mask == rgt.mask;
}

// bool DbType::isEnabled(int features, DbType feature) {
// return (features & feature) != 0;
// }

// u_int64_t DbType::config(int features, DbType feature, bool state) {
// if (state) {
// features |= feature;
// } else {
// features &= ~feature;
// }

// return features;
// }

uint64_t DbType::of(DbType_list_ptr features)
{
  if (features->empty())
  {
    return 0;
  }
  u_int64_t v = 0;
  for (auto p1 = features->begin(); p1 != features->end(); p1++)
  {
    v |= (*p1)->mask;
  }
  return v;
}

DbType_ptr DbType::valueOf(string_ptr dbType)
{
  if (*dbType == "other")
  {
    return DbType_ptr(new DbType(&DbType::other));
  }
  else if (*dbType == "jtds")
  {
    return DbType_ptr(new DbType(&DbType::jtds));
  }
  else if (*dbType == "hsql")
  {
    return DbType_ptr(new DbType(&DbType::hsql));
  }
  else if (*dbType == "db2")
  {
    return DbType_ptr(new DbType(&DbType::db2));
  }
  else if (*dbType == "postgresql")
  {
    return DbType_ptr(new DbType(&DbType::postgresql));
  }
  else if (*dbType == "sqlserver")
  {
    return DbType_ptr(new DbType(&DbType::sqlserver));
  }
  else if (*dbType == "oracle")
  {
    return DbType_ptr(new DbType(&DbType::oracle));
  }
  else if (*dbType == "mysql")
  {
    LOG_INFO << "dbType == mysql";
    return DbType_ptr(new DbType(&DbType::mysql));
  }
  else if (*dbType == "mariadb")
  {
    return DbType_ptr(new DbType(&DbType::mariadb));
  }
  else if (*dbType == "derby")
  {
    return DbType_ptr(new DbType(&DbType::derby));
  }
  else if (*dbType == "hive")
  {
    return DbType_ptr(new DbType(&DbType::hive));
  }
  else if (*dbType == "h2")
  {
    return DbType_ptr(new DbType(&DbType::h2));
  }
  else if (*dbType == "dm")
  {
    return DbType_ptr(new DbType(&DbType::dm));
  }
  else if (*dbType == "kingbase")
  {
    return DbType_ptr(new DbType(&DbType::kingbase));
  }
  else if (*dbType == "gbase")
  {
    return DbType_ptr(new DbType(&DbType::gbase));
  }
  else if (*dbType == "oceanbase")
  {
    return DbType_ptr(new DbType(&DbType::oceanbase));
  }
  else if (*dbType == "informix")
  {
    return DbType_ptr(new DbType(&DbType::informix));
  }
  else if (*dbType == "odps")
  {
    return DbType_ptr(new DbType(&DbType::odps));
  }
  else if (*dbType == "teradata")
  {
    return DbType_ptr(new DbType(&DbType::teradata));
  }
  else if (*dbType == "phoenix")
  {
    return DbType_ptr(new DbType(&DbType::phoenix));
  }
  else if (*dbType == "edb")
  {
    return DbType_ptr(new DbType(&DbType::edb));
  }
  else if (*dbType == "kylin")
  {
    return DbType_ptr(new DbType(&DbType::kylin));
  }
  else if (*dbType == "sqlite")
  {
    return DbType_ptr(new DbType(&DbType::sqlite));
  }
  else if (*dbType == "ads")
  {
    return DbType_ptr(new DbType(&DbType::ads));
  }
  else if (*dbType == "presto")
  {
    return DbType_ptr(new DbType(&DbType::presto));
  }
  else if (*dbType == "elastic_search")
  {
    return DbType_ptr(new DbType(&DbType::elastic_search));
  }
  else if (*dbType == "hbase")
  {
    return DbType_ptr(new DbType(&DbType::hbase));
  }
  else if (*dbType == "drds")
  {
    return DbType_ptr(new DbType(&DbType::drds));
  }
  else if (*dbType == "clickhouse")
  {
    return DbType_ptr(new DbType(&DbType::clickhouse));
  }
  else if (*dbType == "blink")
  {
    return DbType_ptr(new DbType(&DbType::blink));
  }
  else if (*dbType == "antspark")
  {
    return DbType_ptr(new DbType(&DbType::antspark));
  }
  else if (*dbType == "oceanbase_oracle")
  {
    return DbType_ptr(new DbType(&DbType::oceanbase_oracle));
  }
  else if (*dbType == "polardb")
  {
    return DbType_ptr(new DbType(&DbType::polardb));
  }
  else if (*dbType == "ali_oracle")
  {
    return DbType_ptr(new DbType(&DbType::ali_oracle));
  }
  else if (*dbType == "mock")
  {
    return DbType_ptr(new DbType(&DbType::mock));
  }
  else if (*dbType == "sybase")
  {
    return DbType_ptr(new DbType(&DbType::sybase));
  }
  else if (*dbType == "highgo")
  {
    return DbType_ptr(new DbType(&DbType::highgo));
  }
  else if (*dbType == "greenplum")
  {
    return DbType_ptr(new DbType(&DbType::greenplum));
  }
  else if (*dbType == "gaussdb")
  {
    return DbType_ptr(new DbType(&DbType::gaussdb));
  }
  else if (*dbType == "trino")
  {
    return DbType_ptr(new DbType(&DbType::trino));
  }
  else if (*dbType == "oscar")
  {
    return DbType_ptr(new DbType(&DbType::oscar));
  }
  else if (*dbType == "tidb")
  {
    return DbType_ptr(new DbType(&DbType::tidb));
  }
  else if (*dbType == "tydb")
  {
    return DbType_ptr(new DbType(&DbType::tydb));
  }
  else if (*dbType == "starrocks")
  {
    return DbType_ptr(new DbType(&DbType::starrocks));
  }
  else if (*dbType == "ingres")
  {
    return DbType_ptr(new DbType(&DbType::ingres));
  }
  else if (*dbType == "cloudscape")
  {
    return DbType_ptr(new DbType(&DbType::cloudscape));
  }
  else if (*dbType == "timesten")
  {
    return DbType_ptr(new DbType(&DbType::timesten));
  }
  else if (*dbType == "as400")
  {
    return DbType_ptr(new DbType(&DbType::as400));
  }
  else if (*dbType == "sapdb")
  {
    return DbType_ptr(new DbType(&DbType::sapdb));
  }
  else if (*dbType == "kdb")
  {
    return DbType_ptr(new DbType(&DbType::kdb));
  }
  else if (*dbType == "log4jdbc")
  {
    return DbType_ptr(new DbType(&DbType::log4jdbc));
  }
  else if (*dbType == "xugu")
  {
    return DbType_ptr(new DbType(&DbType::xugu));
  }
  else if (*dbType == "firebirdsql")
  {
    return DbType_ptr(new DbType(&DbType::firebirdsql));
  }
  else if (*dbType == "JSQLConnect")
  {
    return DbType_ptr(new DbType(&DbType::JSQLConnect));
  }
  else if (*dbType == "JTurbo")
  {
    return DbType_ptr(new DbType(&DbType::JTurbo));
  }
  else if (*dbType == "interbase")
  {
    return DbType_ptr(new DbType(&DbType::interbase));
  }
  else if (*dbType == "pointbase")
  {
    return DbType_ptr(new DbType(&DbType::pointbase));
  }
  else if (*dbType == "edbc")
  {
    return DbType_ptr(new DbType(&DbType::edbc));
  }
  else if (*dbType == "mimer")
  {
    return DbType_ptr(new DbType(&DbType::mimer));
  }
  else
  {
    return DbType_ptr(new DbType(&DbType::other));
  }
}
